package com.mxy.batterychargingreminding;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.support.v4.app.NotificationCompat;

/**
 * 电池变更广播接收器
 *
 * @author 马晓勇
 */

public class BatteryChangeReceiver extends BroadcastReceiver {

    private static final int NOTICE_ID = 121446;
    private static final String NOTIFICATION_CHANNEL_ID = "2";
    private static final String NOTIFICATION_CHANNEL_NAME = "highImportanceChannel";
    private static final String BATTERY_FULL = "BATTERY_FULL";
    private static final String BATTERY_LOW = "BATTERY_LOW";

    @Override
    public void onReceive(Context context, Intent intent) {

        NotificationManager notificationManager =
                (NotificationManager) context.getApplicationContext()
                        .getSystemService(Context.NOTIFICATION_SERVICE);

        Util.print(intent.getAction());
        Util.print("当前电量百分比: " + intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0));
        Util.print("电池温度: " + (float) (intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,
                0) / 10) + "度");


        switch (intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1)) {
            case BatteryManager.BATTERY_HEALTH_COLD:
                Util.print("电池健康状况:温度过低");
                break;
            case BatteryManager.BATTERY_HEALTH_DEAD:
                Util.print("电池健康状况:没电");
                break;
            case BatteryManager.BATTERY_HEALTH_GOOD:
                Util.print("电池健康状况:良好");
                break;
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                Util.print("电池健康状况:电压过高");
                break;
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                Util.print("电池健康状况:过热");
                break;
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                Util.print("电池健康状况:电池状态获取失败");
                break;
            case BatteryManager.BATTERY_HEALTH_UNKNOWN:
                Util.print("电池健康状况:未知错误");
                break;
            default:
                break;
        }
        switch (intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                Util.print("手机充电中");
                break;
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                Util.print("手机放电中");
                break;
            case BatteryManager.BATTERY_STATUS_FULL:
                Util.print("电池已满");
                break;
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                Util.print("手机未充电");
                break;
            case BatteryManager.BATTERY_STATUS_UNKNOWN:
                Util.print("手机状态未知");
                break;
            default:
                break;
        }
        switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                Util.print("充电器充电");
                break;
            case BatteryManager.BATTERY_PLUGGED_USB:
                Util.print("USB充电");
                break;
            case BatteryManager.BATTERY_PLUGGED_WIRELESS:
                Util.print("无线充电");
                break;
            default:
                break;
        }

        // 电量100并且充电中
        if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())
                && intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) == BatteryManager.BATTERY_STATUS_FULL
                && intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0) {

            notice(context, notificationManager, BATTERY_FULL);
        } else if (Intent.ACTION_BATTERY_LOW.equals(intent.getAction())
                && intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) == 0) {

            // 手机亏电并且手机未充电
            notice(context, notificationManager, BATTERY_LOW);
        } else if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())
                || Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {

            // 连通电源或断开电源
            if (notificationManager != null) {
                notificationManager.cancelAll();
            }
        }
    }

    /**
     * 发送通知
     *
     * @param context
     * @param notificationManager
     */
    private void notice(Context context, NotificationManager notificationManager, String type) {

        NotificationCompat.Builder builder = Util.getNotificationBuilder(context,
                NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, true);

        if (builder != null) {

            if (BATTERY_FULL.equals(type)) {
                builder.setTicker(context.getResources().getString(R.string.full_battery));
                builder.setContentText(context.getResources().getString(R.string.full_battery));
            } else {
                builder.setTicker(context.getResources().getString(R.string.low_battery));
                builder.setContentText(context.getResources().getString(R.string.low_battery));
            }

            builder.setSmallIcon(R.drawable.small_icon);
            builder.setContentTitle(context.getString(R.string.app_name));
            //发送时间
            builder.setWhen(System.currentTimeMillis());
            builder.setDefaults(NotificationCompat.DEFAULT_ALL);
            builder.setPriority(NotificationCompat.PRIORITY_MAX);
            builder.setAutoCancel(true);
            Notification notification = builder.build();

            if (notificationManager != null) {
                notificationManager.notify(NOTICE_ID, notification);
            }
        }
    }
}